import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { useDataSourceStore } from './dataSource'

// 模拟API延迟
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

// 数据集类型选项
export const datasetTypeOptions = [
  { value: 'table', label: '表' },
  { value: 'view', label: '视图' },
  { value: 'query', label: '自定义查询' },
]

// 同步频率选项
export const syncFrequencyOptions = [
  { value: 'manual', label: '手动' },
  { value: 'hourly', label: '每小时' },
  { value: 'daily', label: '每天' },
  { value: 'weekly', label: '每周' },
]

// 同步类型选项
export const syncTypeOptions = [
  { value: 'full', label: '全量同步' },
  { value: 'incremental', label: '增量同步' },
]

export const useDataSetStore = defineStore('dataSet', () => {
  // 获取数据源 store
  const dataSourceStore = useDataSourceStore()

  // 状态
  const datasets = ref([])
  const loading = ref(false)
  const syncing = ref(false)

  // 初始化一些示例数据
  const initSampleData = async () => {
    loading.value = true
    await delay(500)

    // 确保数据源已加载
    await dataSourceStore.fetchSources()

    datasets.value = [
   
    ]

    loading.value = false
  }

  // 获取数据集列表
  const fetchDatasets = async () => {
    if (datasets.value.length === 0) {
      await initSampleData()
    }
    return datasets.value
  }

  // 获取单个数据集
  const getDatasetById = (id) => {
    return datasets.value.find((dataset) => dataset.id === id)
  }

  // 根据数据源ID获取数据集
  const getDatasetsBySourceId = (sourceId) => {
    return datasets.value.filter((dataset) => dataset.sourceId === sourceId)
  }

  // 创建数据集
  const createDataset = async (datasetData) => {
    loading.value = true
    await delay(800)

    const newDataset = {
      ...datasetData,
      id: datasets.value.length + 1,
      syncStatus: 'pending',
      lastSyncTime: null,
      createTime: new Date().toLocaleString(),
      updateTime: new Date().toLocaleString(),
    }

    datasets.value.push(newDataset)
    loading.value = false

    ElMessage.success('数据集创建成功')
    return newDataset
  }

  // 更新数据集
  const updateDataset = async (id, datasetData) => {
    loading.value = true
    await delay(800)

    const index = datasets.value.findIndex((dataset) => dataset.id === id)
    if (index !== -1) {
      datasets.value[index] = {
        ...datasets.value[index],
        ...datasetData,
        updateTime: new Date().toLocaleString(),
      }

      loading.value = false
      ElMessage.success('数据集更新成功')
      return datasets.value[index]
    }

    loading.value = false
    ElMessage.error('未找到数据集')
    return null
  }

  // 删除数据集
  const deleteDataset = async (id) => {
    loading.value = true
    await delay(600)

    const index = datasets.value.findIndex((dataset) => dataset.id === id)
    if (index !== -1) {
      datasets.value.splice(index, 1)
      loading.value = false
      ElMessage.success('数据集删除成功')
      return true
    }

    loading.value = false
    ElMessage.error('未找到数据集')
    return false
  }

  // 同步数据集
  const syncDataset = async (id) => {
    syncing.value = true
    await delay(2000)

    const index = datasets.value.findIndex((dataset) => dataset.id === id)
    if (index !== -1) {
      // 随机模拟同步成功或失败
      const syncStatus = Math.random() > 0.2 ? 'success' : 'failed'
      const now = new Date().toLocaleString()

      datasets.value[index] = {
        ...datasets.value[index],
        syncStatus,
        lastSyncTime: now,
        updateTime: now,
      }

      syncing.value = false

      if (syncStatus === 'success') {
        ElMessage.success('数据集同步成功')
      } else {
        ElMessage.error('数据集同步失败')
      }

      return { success: syncStatus === 'success', dataset: datasets.value[index] }
    }

    syncing.value = false
    ElMessage.error('未找到数据集')
    return { success: false, dataset: null }
  }

  // 计算属性
  const datasetOptions = computed(() => {
    return datasets.value.map((dataset) => ({
      value: dataset.id,
      label: dataset.name,
      sourceId: dataset.sourceId,
    }))
  })

  // 获取数据集的数据源信息
  const getDatasetSource = (datasetId) => {
    const dataset = getDatasetById(datasetId)
    if (dataset) {
      return dataSourceStore.getSourceById(dataset.sourceId)
    }
    return null
  }

  return {
    datasets,
    loading,
    syncing,
    fetchDatasets,
    getDatasetById,
    getDatasetsBySourceId,
    createDataset,
    updateDataset,
    deleteDataset,
    syncDataset,
    datasetOptions,
    getDatasetSource,
  }
})
